package com.j283_5.wisdom_park.controller.device_management;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.j283_5.wisdom_park.common.result.ResultData;
import com.j283_5.wisdom_park.service.DeviceInformationService;
import com.j283_5.wisdom_park.vo.device_management.DeviceInformationVo;
import com.j283_5.wisdom_park.vo.device_management.DeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wsir
 * @since 2022-04-28
 */


@Transactional
@RestController
@RequestMapping("/device")
@Api(value = "设备管理信息",tags = "设备信息管理",produces="王银伟")
public class DeviceInformationController {

    @Resource
    private DeviceInformationService deviceInformationService;

    @RequestMapping(value = "/deviceInformation" , method = RequestMethod.POST)
    @ApiOperation(value = "查询所有设备信息",notes = "查询所有设备信息（分页）",produces="王银伟", response = ResultData.class)
    public ResultData deviceInformationList(@Param("page") Page<DeviceInformationVo> page, @Param("deviceInformationVo") DeviceInformationVo deviceInformationVo){
        //将需要模糊查询的条件存储到map中
        Map<String,Object> map = new HashMap();

        List<DeviceInformationVo> deviceInformationVoList = deviceInformationService.queryDeviceInformation(page, deviceInformationVo,0);
        if (!(deviceInformationVoList != null && deviceInformationVoList.size() > 0)){
            return ResultData.error();
        }

        Integer sum = deviceInformationService.queryDeviceCount(deviceInformationVo,0);
        map.put("deviceInformationVoList",deviceInformationVoList);
        map.put("sum",sum);

        return ResultData.ok().data(map);
    }

    @RequestMapping(value = "/deviceDetails" , method = RequestMethod.POST)
    @ApiOperation(value = "查询设备详细信息",notes = "查询设备详细信息",produces="王银伟", response = ResultData.class)
    public ResultData deviceDetails(Integer dinfId){
        return ResultData.ok().data(deviceInformationService.queryOneDevice(dinfId));
    }

    @RequestMapping(value = "/deviceDelete" , method = RequestMethod.POST)
    @ApiOperation(value = "删除设备信息",notes = "删除设备信息以及批量删除",produces="王银伟", response = ResultData.class)
    public ResultData deviceDelete(@RequestBody ArrayList<Integer> deviceIdList){
//        ArrayList<Integer> deviceList = new ArrayList();
//        deviceIdList.forEach((v)->{
//            deviceList.add(Integer.valueOf(v));
//        });

        if(deviceInformationService.deleteDevice(deviceIdList)){
            return ResultData.ok().data("删除成功");
        }
        return  ResultData.error().message("删除失败");
    }

    @RequestMapping(value = "/deviceAdd" , method = RequestMethod.POST)
    @ApiOperation(value = "添加设备信息",notes = "添加设备信息",produces="王银伟", response = ResultData.class)
    public ResultData addDevice(DeviceVo deviceVo){
        if (deviceInformationService.addDeviceInformation(deviceVo)){
            return ResultData.ok().data("添加成功");
        }
        return ResultData.error().message("添加失败");
    }

    @RequestMapping(value = "/projectAdminId" , method = RequestMethod.POST)
    @ApiOperation(value = "获取管理员信息，用于下拉框显示",notes = "获取管理员信息，用于下拉框显示",produces="王银伟", response = ResultData.class)
    public ResultData projectAdminId(){

        List<Map> list = new ArrayList<>();

//        list.forEach((v)->{
//            HashMap<String, Object> map = new HashMap<>();
//
//            map.put("value","1");
//            map.put("name","wyw");
//            list.add(map);
//        });
        for (int i = 1; i < 10; i++) {
            HashMap<String, Object> map = new HashMap<>();

            map.put("value",i);
            map.put("name","wyw"+i);
            list.add(map);
        }

        return ResultData.ok().data(list);
    }

}


